<script lang="ts" setup>
import { ref, nextTick } from 'vue'
import wjzs from '@/components/wjzs.vue'
import gdglService from '@/api/gdgl'
import wjfwService from '@/api/wjfw'
import { ElMessage } from 'element-plus'
import { useMainStore } from '@/store/main'
import printFn from '@/utils/print'
const store = useMainStore()
/*
 * 打开弹框
 */
//
const dialogVisible = ref<boolean>(false)
const objDataParams = ref()
// 入参
const wjzsRef = ref<InstanceType<typeof wjzs>>()
// 生命周期
const openDialog = (val: any) => {
  objDataParams.value = val
  getSxjcbgwjxh(val.dabh)
  dialogVisible.value = true
}
/**
 * 获取归档顺序
 */

const url = ref<string>('')
const loading = ref(false)
const getSxjcbgwjxh = (val: any) => {
  const params = {
    dabh: val,
  }
  loading.value = true
  gdglService
    .getSxjcbgwjxh(params)
    .then((res) => {
      if (res) {
        const xh = res.data.wjxh as unknown as string
        getJcbg(xh)
      }
    })
    .catch((err) => {
      !err.isError && ElMessage({ type: 'warning', message: err.message })
      loading.value = false
    })
}

const getJcbg = (val: string) => {
  const params = {
    wjbh: val,
  }
  wjfwService.downloadBlob(params).then((res) => {
    if (res.type === 'application/json') {
      const reader = new FileReader()
      reader.onload = () => {
        const jsonData =
          typeof reader.result === 'string' ? JSON.parse(reader.result) : reader.result
        ElMessage({ type: 'warning', message: jsonData.message })
      }
      reader.readAsText(res)
    } else {
      url.value = window.URL.createObjectURL(res)
      nextTick(() => {
        wjzsRef.value?.init()
      })
    }
    loading.value = false
  })
}

// 下载按钮
const download = () => {
  const params = {
    dabh: objDataParams.value.dabh,
  }
  gdglService
    .getSxjcbgwjxh(params)
    .then((res) => {
      if (res) {
        const xh = res.data.wjxh as unknown as string
        window.location.href = `${window.IPCONFIG}/fpi/wjfw/download?wjbh=${xh}`
      }
    })
    .catch((err) => {
      !err.isError && ElMessage({ type: 'warning', message: err.message })
    })
}

const print = () => {
  const params = {
    fileSource: '2',
    localFilePathList: [],
    fileName: '四性检测报告.pdf',
    loadingHeader: {},
    loadingParam: {},
    loadingUrl: `${window.IPCONFIG}/fpi/wjfw/download`,
    requestMethodName: 'GET',
    preUrl: `${window.IPCONFIG}/api/gdgl/getSxjcbgwjxh`,
    preMethodName: 'POST',
    preParam: {
      gdbhList: objDataParams.value,
    },
    preResField: 'gddjbxh',
  }
  printFn(params)
}

defineExpose({ openDialog })
</script>
<template>
  <el-dialog v-model="dialogVisible" title="检测结果" width="80%" :close-on-click-modal="false">
    <div class="dialog-box" v-loading="loading">
      <!--文件-->
      <wjzs ref="wjzsRef" wjlx="pdf" :url="url" />
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="download">下 载</el-button>
        <el-button type="primary" @click="print">打 印</el-button>
        <el-button @click="dialogVisible = false">关 闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style lang="scss" scoped>
.dialog-box {
  width: 100%;
  height: 70vh;
}
</style>
